/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.teach.web;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.resource.entity.ResourceCourse;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSet;
import com.lczyfz.istep.modules.resource.entity.ResourcePaper;
import com.lczyfz.istep.modules.resource.entity.ResourceTrainprogram;
import com.lczyfz.istep.modules.resource.service.*;
import com.lczyfz.istep.modules.sys.service.SystemService;
import com.lczyfz.istep.modules.sys.utils.UserUtils;
import com.lczyfz.istep.modules.teach.entity.TeachDistribute;
import com.lczyfz.istep.modules.teach.entity.TeachExam;
import com.lczyfz.istep.modules.teach.service.TeachDistributeService;
import com.lczyfz.istep.modules.teach.service.TeachExamService;
import com.lczyfz.istep.modules.teach.service.TeachStudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

/**
 * 我的学习Controller
 * @author cqh
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/stu/stuLearn")
public class StuLearnController extends BaseController {

	@Autowired
	private TeachDistributeService teachDistributeService;
	@Autowired
	private ResourceCourseService resourceCourseService;
	@Autowired
	private ResourceCtfSetService resourceCtfSetService;
	@Autowired
	private ResourcePaperService resourcePaperService;
	@Autowired
	private TeachStudentService teachStudentService;
	@Autowired
	private TeachExamService teachExamService;

	@ModelAttribute
	public TeachDistribute get(@RequestParam(required=false) String id) {
		TeachDistribute entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = teachDistributeService.get(id);
		}
		if (entity == null){
			entity = new TeachDistribute();
		}
		return entity;
	}

	//我的学习任务列表展示
	@RequestMapping(value = {"list", ""})
	public String list(TeachDistribute teachDistribute, HttpServletRequest request, HttpServletResponse response, Model model) {
		teachDistribute.setUserId(UserUtils.getUser().getId());
		Page<TeachDistribute> page = teachDistributeService.findStuLearnList(new Page<TeachDistribute>(request, response), teachDistribute);
		model.addAttribute("page", page);
		return "modules/teach/stuLearnList";
	}

	//我的学习任务-获取课程名称
	@RequestMapping(value = "stuLearnDetail")
	public String stuLearnDetail(String distributeId, Model model){
		List<ResourceCourse> stuLearnCourse = resourceCourseService.stuLearnCourse(distributeId, UserUtils.getUser().getId());
		List<ResourceCtfSet> stuLearnCtfSet = resourceCtfSetService.stuLearnCtfSet(distributeId, UserUtils.getUser().getId());
		//根据distributeId和学生id获取该次下发的考试
		List<TeachExam> stuLearnExam = teachExamService.stuLearnExam(distributeId, UserUtils.getUser().getId());

		logger.debug("=Evan===stuLearnDetail===distributeId: "+distributeId);
//		logger.debug("=Evan===stuLearnDetail===getPaperId: "+stuLearnExam.get(0).getPaperId());

		//根据学生id获取所有下发给该学生的考试，如果某学生接受了两次下发有同样的试卷，则保留最早的考试
		//这里返回的结果也即‘我的考试’展示的列表
		TeachExam teachExam = new TeachExam();
		teachExam.setUserId(UserUtils.getUser().getId());
		List<TeachExam> stuExamList = teachExamService.stuExamList(teachExam);

		logger.debug("=Evan===stuLearnDetail===stuExamList: "+stuExamList);

		//将stuLearnExam和stuExamList进行循环比较
		//如果stuLearnExam的某条记录的getPaperId与stuExamList的某条记录的getPaperId相同，并且
		//stuLearnExam的那条记录的getDistributeId与stuExamList的那条记录的getDistributeId不同，那么
		//把stuLearnExam中的该条记录删除
		for(int i=0; i<stuLearnExam.size(); i++){
			for(int j=0; j<stuExamList.size(); j++){
				if(stuLearnExam.size() == 0){
					break;
				}else {
					if(stuExamList.get(j).getPaperId().equals(stuLearnExam.get(i).getPaperId()) && !stuExamList.get(j).getDistributeId().equals(distributeId)){
						logger.debug("=Evan===stuExamList===getPaperId: "+stuExamList.get(j).getPaperId()+"  getDistributeId: "+stuExamList.get(j).getDistributeId());
						stuLearnExam.remove(i);
					}
				}
			}
		}
		model.addAttribute("stuLearnCourse", stuLearnCourse);
		model.addAttribute("stuLearnCtfSet", stuLearnCtfSet);
		model.addAttribute("stuLearnExam", stuLearnExam);
		return "modules/teach/stuLearnDetail";
	}

	//课件进度操作
	@RequestMapping(value = "LearnProgress")
	public String LearnProgress(int LearnProgress, String CoursewareId){
		teachStudentService.LearnProgress(LearnProgress, CoursewareId);
		return "modules/teach/stuCourseLearn";
	}

	//课件学习-进入实验
	@RequestMapping(value = "enterExperiment")
	public String enterExperiment(){
		return "modules/scene/sceneExperimentOperation";
	}



}